<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;

class ApiBusiness extends ApiCommon {
	public function initialize() {
		parent::initialize();
		$bset = Db::name('business_sysset')->where('aid', aid)->find();
		if ($bset['status'] == 0) {
			die(jsonEncode(['status' => -4, 'msg' => '功能未开启', 'url' => '/pages/index/index']));
		}
	}
	//商家详情页
	public function index($bid = 0) {
		$bid = $bid > 0 ? $bid : input('param.id/d');
		$business = Db::name('business')->where('aid', aid)->where('id', $bid)->where('status', 1)->find();
		if (!$business) {
			return $this->json(['status' => 0, 'msg' => '商家信息不存在']);
		}

		if ($business['is_open'] == 0) {
			return $this->json(['status' => -4, 'msg' => '商家未营业']);
		}

		$is_open = 0;
		if ($is_open == 0) {
			if ($business['start_hours'] != $business['end_hours']) {
				$start_time = strtotime(date('Y-m-d ' . $business['start_hours']));
				$end_time = strtotime(date('Y-m-d ' . $business['end_hours']));
				if (($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))) {
					//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
				} else {
					$is_open = 1;
				}
			} else {
				$is_open = 1;
			}
		}
		if ($is_open == 0) {
			$start_time = strtotime(date('Y-m-d ' . $business['start_hours2']));
			$end_time = strtotime(date('Y-m-d ' . $business['end_hours2']));
			if ($start_time == $end_time || ($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))) {
				//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
			} else {
				$is_open = 1;
			}
		}
		if ($is_open == 0) {
			$start_time = strtotime(date('Y-m-d ' . $business['start_hours3']));
			$end_time = strtotime(date('Y-m-d ' . $business['end_hours3']));
			if ($start_time == $end_time || ($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))) {
				//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
			} else {
				$is_open = 1;
			}
		}
		if ($is_open == 0) {
			return $this->json(['status' => -4, 'msg' => '商家不在营业时间']);
		}
		$countcomment = Db::name('business_comment')->where('aid', aid)->where('bid', $bid)->where('status', 1)->count();
		$couponcount = Db::name('coupon')->where('aid', aid)->where('bid', $bid)->where('tolist', 1)->order('sort desc,id desc')->count();
		$bset = Db::name('business_sysset')->where('aid', aid)->find();

		$prosales = Db::name('shop_product')->where('bid', $bid)->sum('sales');
		if ($business['sales'] < $prosales) {
			$business['sales'] = $prosales;
		}

		$pagedata = Db::name('designerpage')->where('aid', aid)->where('bid', $bid)->where('ishome', 1)->find();
		if (!$pagedata) {
			$yuyue_clist = Db::name('yuyue_category')->where('pid', 0)->select()->toArray();
			$rdata = [];
			$rdata['isdiy'] = 0;
			$rdata['bset'] = $bset;
			$rdata['business'] = $business;
			$rdata['countcomment'] = $countcomment;
			$rdata['yuyue_clist'] = $yuyue_clist;
			$rdata['couponcount'] = $couponcount;
			$rdata['pics'] = explode(',', $business['pics']);
			$rdata['showfw'] = getcustom('businessindex_showfw');
			return $this->json($rdata);
		}

		$pageinfo = json_decode($pagedata['pageinfo'], true);
		$pagecontent = json_decode(\app\common\System::initpagecontent($pagedata['content'], aid, mid, platform), true);
		$pageparams = $pageinfo[0]['params'];

		$guanggaopic = '';
		$guanggaourl = '';
		if ($pageparams['showgg'] == 1) {
			$showgg = 0;
			if ($pageparams['ggrenqun']) {
				if ($pageparams['ggrenqun']['0']) {
					$showgg = 1;
				}
				if ($pageparams['ggrenqun']['-1'] && $this->member['subscribe'] == 1) {
					$showgg = 1;
				}
				if ($pageparams['ggrenqun']['-2'] && $this->member['subscribe'] != 1) {
					$showgg = 1;
				}
				if ($showgg == 0 && $pageparams['ggrenqun'][$this->member['levelid']]) {
					$showgg = 1;
				}
			}
			if ($showgg == 1 && $pageparams['cishu'] == 0 && $this->member) {
				$hasshowlog = Db::name('guanggao_showlog')->where('mid', mid)->where('pic', $pageparams['guanggao'])->find();
				if ($hasshowlog) {
					$showgg = 0;
				} else {
					Db::name('guanggao_showlog')->insert(['aid' => aid, 'mid' => mid, 'pic' => $pageparams['guanggao'], 'createtime' => time()]);
				}
			}
			if ($showgg) {
				$guanggaopic = $pageparams['guanggao'];
				$guanggaourl = $pageparams['hrefurl'];
			}
		}
		$admin_set = Db::name('admin_set')->field('name,logo,desc,tel,gzts,ddbb,mode')->where('aid', aid)->find();

		$sysset = ['name' => $business['name'], 'logo' => $business['logo'], 'desc' => $business['address'], 'tel' => $business['tel'], 'mode' => $admin_set['mode'], 'address' => $business['address']];

		if (!$pageparams['bgcolor']) {
			$pageparams['bgcolor'] = '#f7f7f8';
		}
		$yuyue_clist = Db::name('yuyue_category')->where('pid', 0)->select()->toArray();

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['isdiy'] = 1;
		$rdata['bset'] = $bset;
		$rdata['guanggaopic'] = $guanggaopic;
		$rdata['guanggaourl'] = $guanggaourl;
		$rdata['pageinfo'] = $pageparams;
		$rdata['pagecontent'] = $pagecontent;
		$rdata['business'] = $business;
		$rdata['sysset'] = $sysset;
		$rdata['yuyue_clist'] = $yuyue_clist;

		return $this->json($rdata);
	}

	public function main() {
		$pageid = input('param.id/d');
		$pagedata = Db::name('designerpage')->where('aid', aid)->where('id', $pageid)->find();
		if (!$pagedata) {
			return $this->json(['status' => 0, 'msg' => '页面不存在']);
		}
		$bid = $pagedata['bid'];

		$business = Db::name('business')->where('aid', aid)->where('id', $bid)->where('status', 1)->find();
		$pageinfo = json_decode($pagedata['pageinfo'], true);
		$pagecontent = json_decode(\app\common\System::initpagecontent($pagedata['content'], aid, mid, platform), true);
		$pageparams = $pageinfo[0]['params'];
		if ($pageparams['quanxian']) {
			if (!$pageparams['quanxian']['0'] && !$pageparams['quanxian'][$this->member['levelid']]) {
				return $this->json(['status' => 0, 'msg' => '您无查看权限']);
			}
		}
		if ($pageparams['fufei'] == 1 && floatval($pageparams['money']) > 0) {
//付费查看
			$hasff = Db::name('designerpage_order')->where('aid', aid)->where('pageid', $pagedata['id'])->where('mid', mid)->where('status', 1)->find();
			if (!$hasff) {
				$adata = array();
				$adata['aid'] = aid;
				$adata['pageid'] = $pagedata['id'];
				$adata['mid'] = mid;
				$adata['title'] = $pageparams['title'];
				$adata['price'] = floatval($pageparams['money']);
				$adata['ordernum'] = date('ymdHis') . aid . rand(1000, 9999);
				$adata['createtime'] = time();
				$orderid = Db::name('designerpage_order')->insertGetId($adata);
				return $this->json(['status' => 2, 'msg' => '需要付费查看', 'orderid' => $orderid]);
			}
		}

		$guanggaopic = '';
		$guanggaourl = '';
		if ($pageparams['showgg'] == 1) {
			$showgg = 0;
			if ($pageparams['ggrenqun']) {
				if ($pageparams['ggrenqun']['0']) {
					$showgg = 1;
				}
				if ($pageparams['ggrenqun']['-1'] && $this->member['subscribe'] == 1) {
					$showgg = 1;
				}
				if ($pageparams['ggrenqun']['-2'] && $this->member['subscribe'] != 1) {
					$showgg = 1;
				}
				if ($showgg == 0 && $pageparams['ggrenqun'][$this->member['levelid']]) {
					$showgg = 1;
				}
			}
			if ($showgg == 1 && $pageparams['cishu'] == 0 && $this->member) {
				$hasshowlog = Db::name('guanggao_showlog')->where('mid', mid)->where('pic', $pageparams['guanggao'])->find();
				if ($hasshowlog) {
					$showgg = 0;
				} else {
					Db::name('guanggao_showlog')->insert(['aid' => aid, 'mid' => mid, 'pic' => $pageparams['guanggao'], 'createtime' => time()]);
				}
			}
			if ($showgg) {
				$guanggaopic = $pageparams['guanggao'];
				$guanggaourl = $pageparams['hrefurl'];
			}
		}
		$sysset = ['name' => $business['name'], 'logo' => $business['pic'], 'desc' => $business['address'], 'tel' => $business['tel']];

		if (!$pageparams['bgcolor']) {
			$pageparams['bgcolor'] = '#f7f7f8';
		}
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['isdiy'] = 1;
		$rdata['guanggaopic'] = $guanggaopic;
		$rdata['guanggaourl'] = $guanggaourl;
		$rdata['pageinfo'] = $pageparams;
		$rdata['pagecontent'] = $pagecontent;
		//dump($pagecontent);die;
		$rdata['sysset'] = $sysset;
		return $this->json($rdata);
	}
	//获取商品列表 评价列表
	public function getdatalist() {
		$id = input('param.id/d');
		$st = input('param.st/d');
		$pagenum = input('param.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		if ($st == 0) {
//商品
			$pernum = 20;
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['bid', '=', $id];
			//$where[] = ['status','=',1];
			$nowtime = time();
			$nowhm = date('H:i');
			$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

			$prolist = Db::name('shop_product')->where($where)->page($pagenum, $pernum)->order('id desc')->select()->toArray();
			if (!$prolist) {
				$prolist = [];
			}

			if (request()->isPost()) {
				return $this->json(['status' => 1, 'data' => $prolist]);
			}
		} elseif ($st == -1) {
//预约商品
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['ischecked', '=', 1];
			//$where[] = ['status','=',1];
			$nowtime = time();
			$where[] = Db::raw("`status`=1  or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime)");
			$where[] = ['bid', '=', $id];
			$order = 'sort desc,id desc';
			//分类
			if (input('param.yuyue_cid')) {
				$cid = input('post.yuyue_cid/d');
				//子分类
				$clist = Db::name('yuyue_category')->where('aid', aid)->where('pid', $cid)->column('id');
				if ($clist) {
					$clist2 = Db::name('yuyue_category')->where('aid', aid)->where('pid', 'in', $clist)->column('id');
					$cCate = array_merge($clist, $clist2, [$cid]);
					if ($cCate) {
						$whereCid = [];
						foreach ($cCate as $k => $c2) {
							$whereCid[] = "find_in_set({$c2},cid)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(" . $cid . ",cid)");
				}
			}
			if (input('param.keyword')) {
				$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
			}
			$pernum = 10;
			$pagenum = input('post.pagenum');
			if (!$pagenum) {
				$pagenum = 1;
			}

			$datalist = Db::name('yuyue_product')->field("id,pic,name,sales,sell_price,danwei")->where($where)->page($pagenum, $pernum)->order($order)->select()->toArray();
			if (!$datalist) {
				$datalist = [];
			}

			$datalist = $this->formatprolist($datalist);
			return $this->json(['status' => 1, 'data' => $datalist]);
		} else {
//评价
			$pernum = 10;
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['bid', '=', $id];
			$where[] = ['status', '=', 1];
			$commentlist = Db::name('business_comment')->where($where)->page($pagenum, $pernum)->order('id desc')->select()->toArray();
			if (!$commentlist) {
				$commentlist = [];
			}

			foreach ($commentlist as $k => $pl) {
				$commentlist[$k]['createtime'] = date('Y-m-d H:i', $pl['createtime']);
				if ($commentlist[$k]['content_pic']) {
					$commentlist[$k]['content_pic'] = explode(',', $commentlist[$k]['content_pic']);
				}

			}
			if (request()->isPost()) {
				return $this->json(['status' => 1, 'data' => $commentlist]);
			}
		}
	}
	//商家优惠券
	public function couponlist() {
		//商家优惠券
		$couponlist = Db::name('coupon')->where('aid', aid)->where('bid', $id)->where("unix_timestamp(starttime)<=" . time() . " and unix_timestamp(endtime)>=" . time())->order('sort desc,id desc')->select()->toArray();
		if (!$couponlist) {
			$couponlist = [];
		}

		foreach ($couponlist as $k => $v) {
			$haveget = Db::name('coupon_record')->where('aid', aid)->where('mid', mid)->where('couponid', $v['id'])->count();
			$couponlist[$k]['haveget'] = $haveget;
			$couponlist[$k]['starttime'] = date('m-d H:i', strtotime($v['starttime']));
			$couponlist[$k]['endtime'] = date('m-d H:i', strtotime($v['endtime']));
		}
		$rdata = [];
		$rdata['couponlist'] = $couponlist;
		return $this->json($rdata);
	}
	//分类商家
	public function clist() {
		$clist = Db::name('business_category')->where('aid', aid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		if (request()->isPost()) {
			$cid = input('param.cid/d');
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['status', '=', 1];
			//分类
			if ($cid) {
				$where[] = Db::raw('find_in_set(' . $cid . ',cid)'); //['cid','=',$cid];
			} else {
				//$where[] = Db::raw('find_in_set('.$clist[0]['id'].',cid)'); // ['cid','=',$clist[0]['id']];
			}
			if (input('param.keyword')) {
				$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
			}
			$nowhm = date('H:i');
			$where[] = Db::raw("(start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm')) or (start_hours2<end_hours2 and start_hours2<='$nowhm' and end_hours2>='$nowhm') or (start_hours2>end_hours2 and (start_hours2<='$nowhm' or end_hours2>='$nowhm')) or (start_hours3<end_hours3 and start_hours3<='$nowhm' and end_hours3>='$nowhm') or (start_hours3>end_hours3 and (start_hours3<='$nowhm' or end_hours3>='$nowhm'))");

			$pernum = 12;
			$pagenum = input('post.pagenum');
			if (!$pagenum) {
				$pagenum = 1;
			}

			$longitude = input('post.longitude');
			$latitude = input('post.latitude');
			if ($longitude && $latitude) {
				$orderBy = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) ");
			} else {
				$orderBy = 'sort desc,id';
			}
			$datalist = Db::name('business')->field("id,name,logo,desc,tel,address,longitude,latitude")->where($where)->order($orderBy)->page($pagenum, $pernum)->select()->toArray();
			if (!$datalist) {
				$datalist = array();
			}

			if ($this->member) {
				$memberlevel = Db::name('member_level')->where('id', $this->member['levelid'])->find();
			} else {
				$memberlevel = [];
			}
			foreach ($datalist as $k => $v) {
				if ($longitude && $latitude) {
					$datalist[$k]['juli'] = (getdistance($longitude, $latitude, $v['longitude'], $v['latitude'], 2)) . 'km';
				} else {
					$datalist[$k]['juli'] = '';
				}
				if ($memberlevel && $memberlevel['notshowbtel'] == 1) {
					$datalist[$k]['tel'] = Db::name('admin_set')->where('aid', aid)->value('tel');
				}
			}
			return $this->json(['status' => 1, 'data' => $datalist]);
		}
		$rdata = [];
		$rdata['clist'] = $clist;
		return $this->json($rdata);
	}
	//商家列表
	public function blist() {
		if (request()->isPost()) {
			$pernum = 10;
			$pagenum = input('post.pagenum/d');
			if (!$pagenum) {
				$pagenum = 1;
			}

			$cid = input('post.cid/d');
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['status', '=', 1];
			$where[] = ['is_open', '=', 1];
			if ($cid) {
				$where[] = Db::raw('find_in_set(' . $cid . ',cid)');
			}
			// ['cid','=',$cid];
			if (input('param.keyword')) {
				$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
			}
			$nowhm = date('H:i');
			$where[] = Db::raw("(start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm')) or (start_hours2<end_hours2 and start_hours2<='$nowhm' and end_hours2>='$nowhm') or (start_hours2>end_hours2 and (start_hours2<='$nowhm' or end_hours2>='$nowhm')) or (start_hours3<end_hours3 and start_hours3<='$nowhm' and end_hours3>='$nowhm') or (start_hours3>end_hours3 and (start_hours3<='$nowhm' or end_hours3>='$nowhm'))");

			$latitude = input('param.latitude');
			$longitude = input('param.longitude');
			if ($longitude && $latitude) {
				$order = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) ");
			} else {
				$order = 'sort desc,id desc';
			}
			$field = input('param.field');
			if ($field && $field != 'juli') {
				$order = $field . ' ' . input('param.order') . ',id desc';
			}
			$datalist = Db::name('business')->where($where)->field('id,logo,name,tel,sales,address,latitude,longitude,comment_score')->page($pagenum, $pernum)->order($order)->select()->toArray();
			$nowtime = time();
			$nowhm = date('H:i');
			if (!$datalist) {
				$datalist = array();
			}

			foreach ($datalist as $k => $v) {
				$statuswhere = "`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )";
				if (false) {} else {
					$prolist = Db::name('shop_product')->where('bid', $v['id'])->where($statuswhere)->field('id,pic,name,sales,market_price,sell_price')->limit(4)->order('sort desc,id desc')->select()->toArray();
				}
				if (!$prolist) {
					$prolist = array();
				}

				$v['prolist'] = $prolist;
				if (getcustom('restaurant')) {
					$restaurantProlist = Db::name('restaurant_product')->where('bid', $v['id'])->where($statuswhere)->field('id,pic,name,sales,market_price,sell_price')->limit(4)->order('sort desc,id desc')->select()->toArray();
					if (!$restaurantProlist) {
						$restaurantProlist = array();
					}

					$v['restaurantProlist'] = $restaurantProlist;
				}
				if ($longitude && $latitude) {
					$v['juli'] = '' . getdistance($longitude, $latitude, $v['longitude'], $v['latitude'], 2) . 'km';
				} else {
					$v['juli'] = '';
				}
				$prosales = Db::name('shop_product')->where('bid', $v['id'])->sum('sales');
				if ($v['sales'] < $prosales) {
					$v['sales'] = $prosales;
				}

				$datalist[$k] = $v;
			}
			return $this->json(['status' => 1, 'data' => $datalist]);
		}
		//分类
		$clist = Db::name('business_category')->where('aid', aid)->where('status', 1)->field('id,name,pic')->order('sort desc,id')->select()->toArray();

		$rdata = [];
		$rdata['clist'] = $clist;
		$rdata['showtype'] = 0;
		return $this->json($rdata);
	}
	//商家列表
	public function blist2() {
		$pernum = 15;
		$pagenum = input('post.pagenum/d');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$cid = input('post.cid/d');
		$where = [];
		$where[] = ['aid', '=', aid];
		$where[] = ['status', '=', 1];
		$where[] = ['is_open', '=', 1];
		if ($cid) {
			$where[] = Db::raw('find_in_set(' . $cid . ',cid)');
		}
		// ['cid','=',$cid];
		if (input('param.keyword')) {
			$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
		}
		$nowhm = date('H:i');
		$where[] = Db::raw("(start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm')) or (start_hours2<end_hours2 and start_hours2<='$nowhm' and end_hours2>='$nowhm') or (start_hours2>end_hours2 and (start_hours2<='$nowhm' or end_hours2>='$nowhm')) or (start_hours3<end_hours3 and start_hours3<='$nowhm' and end_hours3>='$nowhm') or (start_hours3>end_hours3 and (start_hours3<='$nowhm' or end_hours3>='$nowhm'))");

		$latitude = input('param.latitude');
		$longitude = input('param.longitude');
		if ($longitude && $latitude) {
			$order = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) ");
		} else {
			$order = 'sort desc,id desc';
		}
		$field = input('param.field');
		if ($field && $field != 'juli') {
			$order = $field . ' ' . input('param.order') . ',id desc';
		}
		$datalist = Db::name('business')->where($where)->field('id,logo,name,sales,address,latitude,longitude,comment_score,tel')->page($pagenum, $pernum)->order($order)->select()->toArray();
		$nowtime = time();
		$nowhm = date('H:i');
		if (!$datalist) {
			$datalist = array();
		}

		foreach ($datalist as $k => $v) {
			$statuswhere = "`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )";
//                $prolist = Db::name('shop_product')->where('bid',$v['id'])->where($statuswhere)->field('id,pic,name,sales,market_price,sell_price')->limit(4)->order('sort desc,id desc')->select()->toArray();
			if (!$prolist) {
				$prolist = array();
			}

			$v['prolist'] = $prolist;
			if (getcustom('restaurant')) {
//                    $restaurantProlist = Db::name('restaurant_product')->where('bid',$v['id'])->where($statuswhere)->field('id,pic,name,sales,market_price,sell_price')->limit(4)->order('sort desc,id desc')->select()->toArray();
				if (!$restaurantProlist) {
					$restaurantProlist = array();
				}

				$v['restaurantProlist'] = $restaurantProlist;
			}
			if ($longitude && $latitude) {
				$v['juli'] = '' . getdistance($longitude, $latitude, $v['longitude'], $v['latitude'], 2) . 'km';
			} else {
				$v['juli'] = '';
			}
//                $prosales = Db::name('shop_product')->where('bid',$v['id'])->sum('sales');
			//                if($v['sales'] < $prosales) $v['sales'] = $prosales;
			$datalist[$k] = $v;
		}
		return $this->json(['status' => 1, 'data' => $datalist]);
	}
	//入驻申请
	public function apply() {
		$this->checklogin();
		if (request()->isPost()) {
			$formdata = input('post.info/a');

			$hasun = Db::name('admin_user')->where('id', '<>', $formdata['id'])->where('un', $formdata['un'])->find();
			if ($hasun) {
				return $this->json(['status' => 0, 'msg' => '该账号已存在']);
			}
			$info = [];
			$info['aid'] = aid;
			$info['mid'] = mid;
			$info['cid'] = $formdata['cid'];
			$info['name'] = $formdata['name'];
			$info['desc'] = $formdata['desc'];
			$info['linkman'] = $formdata['linkman'];
			$info['linktel'] = $formdata['tel'];
			$info['tel'] = $formdata['tel'];
			$info['logo'] = $formdata['pic'];
			$info['pics'] = $formdata['pics'];
			$info['content'] = $formdata['content'];
			$info['address'] = $formdata['address'];
			$info['latitude'] = $formdata['latitude'];
			$info['longitude'] = $formdata['longitude'];
			$info['zhengming'] = $formdata['zhengming'];
			$info['status'] = 0;
			$info['createtime'] = time();
			$info['feepercent'] = Db::name('business_sysset')->where('aid', aid)->value('default_rate');

			$uinfo = [];
			$uinfo['un'] = $formdata['un'];
			$uinfo['pwd'] = $formdata['pwd'];
			if ($formdata['id']) {
				Db::name('business')->where('aid', aid)->where('mid', mid)->where('id', $formdata['id'])->update($info);
				if ($uinfo['pwd'] != '') {
					$uinfo['pwd'] = md5($uinfo['pwd']);
				} else {
					unset($uinfo['pwd']);
				}
				Db::name('admin_user')->where('aid', aid)->where('bid', $info['id'])->where('id', $uinfo['id'])->update($uinfo);
			} else {
				$bid = Db::name('business')->insertGetId($info);
				$uinfo['aid'] = aid;
				$uinfo['bid'] = $bid;
				$uinfo['mid'] = mid;
				$uinfo['auth_type'] = 1;
				$uinfo['pwd'] = md5($uinfo['pwd']);
				$uinfo['createtime'] = time();
				$uinfo['isadmin'] = 1;
				$uinfo['random_str'] = random(16);
				$id = Db::name('admin_user')->insertGetId($uinfo);
			}
			return $this->json(['status' => 1, 'msg' => '提交成功,请等待审核']);
		}
		$info = Db::name('business')->where('aid', aid)->where('mid', mid)->find();
		if ($info && $info['status'] == 1) {
			return $this->json(['status' => 2, 'msg' => '您已成功入驻']);
		}
		$clist = Db::name('business_category')->where('aid', aid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		$bset = Db::name('business_sysset')->where('aid', aid)->field('xieyi_show,xieyi')->find();

		$rdata = [];
		$rdata['title'] = '申请入驻';
		$rdata['clist'] = $clist;
		$rdata['bset'] = $bset;
		$rdata['info'] = $info ? $info : [];
		return $this->json($rdata);
	}

	//商品搜索
	public function search() {
		$bid = input('param.bid/d');
		//分类
		if (input('param.cid')) {
			$clist = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', input('param.cid/d'))->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

		} else {
			$clist = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

		}
		//分组
		//$glist = Db::name('shop_group')->where('aid',aid)->where('status',1)->select()->toArray();
		//if(!$glist) $glist = [];

		$productlisttype = cookie('productlisttype');
		if (!$productlisttype) {
			$productlisttype = 'item2';
		}

		$rdata = [];
		$rdata['clist'] = $clist;
		$rdata['glist'] = [];
		$rdata['productlisttype'] = $productlisttype;
		return $this->json($rdata);
	}
	//商品列表
	public function prolist() {
		$bid = input('param.bid/d');
		$where = [];
		$where[] = ['aid', '=', aid];
		$where[] = ['bid', '=', $bid];
		$nowtime = time();
		$nowhm = date('H:i');
		$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

		if (input('param.field') && input('param.order')) {
			$order = input('param.field') . ' ' . input('param.order') . ',sort,id desc';
		} else {
			$order = 'sort desc,id desc';
		}
		//分类
		if (input('param.cid')) {
			$cid = input('param.cid/d');
			$where[] = ['bcid', '=', $cid];
			//子分类
			$clist = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', $cid)->select()->toArray();
			if ($clist) {
				$cateArr = [$cid];
				foreach ($clist as $c) {
					$cateArr[] = $c['id'];
				}
				$where[] = ['bcid', 'in', $cateArr];
			}
		}
		if (input('param.keyword')) {
			$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
		}
		if (input('param.groupid')) {
			$where[] = Db::raw("find_in_set(" . intval(input('param.groupid')) . ",gid)");
		}

		if (input('param.gid')) {
			$where[] = Db::raw("find_in_set(" . intval(input('param.gid')) . ",gid)");
		}

		$pernum = 10;
		$pagenum = input('post.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$datalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint")->where($where)->page($pagenum, $pernum)->order($order)->select()->toArray();
		if (!$datalist) {
			$datalist = array();
		}

		$datalist = $this->formatprolist($datalist);
		if (request()->isPost()) {
			return $this->json(['status' => 1, 'data' => $datalist]);
		}

		//分类
		if (input('param.cid')) {
			$clist = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', input('param.cid/d'))->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

		} else {
			$clist = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

		}

		$productlisttype = cookie('productlisttype');
		if (!$productlisttype) {
			$productlisttype = 'item2';
		}

		$rdata = [];
		$rdata['clist'] = $clist;
		$rdata['glist'] = [];
		$rdata['datalist'] = $datalist;
		$rdata['productlisttype'] = $productlisttype;

		return $this->json($rdata);
	}
	//分类商品
	public function classify() {
		$order = 'sort desc,id desc';
		if (input('param.field') && input('param.order')) {
			$order = input('param.field') . ' ' . input('param.order') . ',sort,id desc';
		} else {
			$order = 'sort desc,id desc';
		}
		$bid = input('param.bid/d');
		$where = [];
		$where[] = ['aid', '=', aid];
		$where[] = ['bid', '=', $bid];
		//$where[] = ['status','=',1];
		$nowtime = time();
		$nowhm = date('H:i');
		$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

		$cid = input('param.cid');

		$clist = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
		foreach ($clist as $k => $v) {
			$rs = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$rs) {
				$rs = [];
			}

			$clist[$k]['child'] = $rs;
		}
		//if(!$cid) $cid = $clist[0]['id'];
		//分类
		if ($cid) {
			$where[] = ['', '=', intval($cid)];
			$title = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('id', $where['bcid'])->order('sort desc,id')->value('name');
			//子分类
			$child = Db::name('business_shop_category')->where('aid', aid)->where('bid', $bid)->where('pid', $where['bcid'])->select()->toArray();
			if ($child) {
				$cateArr = [$where['bcid']];
				foreach ($child as $c) {
					$cateArr[] = $c['id'];
				}
				$where[] = ['bcid', 'in', $cateArr];
			}
		}
		if (input('param.keyword')) {
			$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
		}
		if (input('param.groupid')) {
			$where[] = Db::raw("find_in_set(" . intval(input('param.groupid')) . ",gid)");
		}

		$pernum = 12;
		$pagenum = input('post.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$datalist = Db::name('shop_product')->field("pic,id,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint")->where($where)->order($order)->page($pagenum, $pernum)->select()->toArray();
		if (!$datalist) {
			$datalist = array();
		}

		$datalist = $this->formatprolist($datalist);
		if (request()->isPost()) {
			return $this->json(['status' => 1, 'data' => $datalist]);
		}

		$rdata = [];
		$rdata['clist'] = $clist;
		$rdata['datalist'] = $datalist;
		return $this->json($rdata);
	}
	//获取子分类
	public function getdownclist() {
		$pid = input('param.id/d');
		$clist = Db::name('business_shop_category')->where('aid', aid)->where('pid', $pid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		if (!$clist) {
			$clist = [];
		}

		return $this->json(['status' => 1, 'data' => $clist]);
	}

	//商品评价
	public function commentlist() {
		$bid = input('param.bid/d');
		$pagenum = input('post.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$pernum = 20;
		$where = [];
		$where[] = ['aid', '=', aid];
		$where[] = ['bid', '=', $bid];
		$where[] = ['status', '=', 1];
		$datalist = Db::name('business_comment')->where($where)->page($pagenum, $pernum)->order('id desc')->select()->toArray();
		if (!$datalist) {
			$datalist = [];
		}

		foreach ($datalist as $k => $pl) {
			$datalist[$k]['createtime'] = date('Y-m-d H:i', $pl['createtime']);
			if ($datalist[$k]['content_pic']) {
				$datalist[$k]['content_pic'] = explode(',', $datalist[$k]['content_pic']);
			}

		}

		$business = Db::name('business')->field('id,name,logo,desc,comment_num,comment_score,comment_haopercent')->where('aid', aid)->where('id', $bid)->where('status', 1)->find();
		if (!$business) {
			return $this->json(['status' => 0, 'msg' => '商家信息不存在']);
		}

		$rdata = [];
		$rdata['data'] = $datalist;
		$rdata['business'] = $business;
		return $this->json($rdata);
	}
}